package jnpf.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jnpf.entity.*;
import jnpf.mapper.FileMangeMapper;
import jnpf.model.themefile.HandleResp;
import jnpf.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.model.knowmange.*;
import jnpf.util.GeneraterSwapUtil;
import jnpf.base.model.ColumnDataModel;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.util.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * knowMange
 * 版本： V3.5
 * 版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * 作者： JNPF开发平台组
 * 日期： 2025-05-19
 */
@Service
public class FileMangeServiceImpl extends ServiceImpl<FileMangeMapper, FileMangeEntity> implements FileMangeService{
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private UserProvider userProvider;

    //子表过滤方法
    @Override
    public QueryWrapper<FileMangeEntity> getChild(KnowMangePagination pagination, QueryWrapper<FileMangeEntity> fileMangeQueryWrapper){
    boolean pcPermission = false;
    boolean appPermission = false;
    boolean	isPc = ServletUtil.getHeader("jnpf-origin").equals("pc");
    String ruleQueryJson = isPc?KnowMangeConstant.getColumnData():KnowMangeConstant.getAppColumnData();
    ColumnDataModel dataModel = JsonUtil.getJsonToBean(ruleQueryJson,ColumnDataModel.class);
    String ruleJson = isPc?JsonUtil.getObjectToString(dataModel.getRuleList()):JsonUtil.getObjectToString(dataModel.getRuleListApp());
        if(isPc){
        }
        return fileMangeQueryWrapper;
    }

    @Override
    public void updateHandleByIds(ArrayList<HandleResp> handleOk) {
        if (handleOk == null || handleOk.isEmpty()) {
            return;
        }

        // 批量查询需要更新的实体
        List<String> ids = handleOk.stream().map(HandleResp::getId).collect(Collectors.toList());
        List<FileMangeEntity> entities = this.listByIds(ids);

        // 更新handle和blocks字段
        for (FileMangeEntity entity : entities) {
            HandleResp resp = handleOk.stream()
                    .filter(r -> r.getId().equals(entity.getId()))
                    .findFirst()
                    .orElse(null);
            if (resp != null) {
                // 使用status字段设置handle状态
                entity.setHandle(resp.getStatus());
                entity.setBlocks(resp.getChunkCount());
            }
        }

        // 批量更新
        this.updateBatchById(entities);
    }
}
